package com.ziiwee.reduce.describe.types.impls;

import com.ziiwee.reduce.commons.utils.ReflectUtil;
import com.ziiwee.reduce.describe.FieldDescribe;
import com.ziiwee.reduce.describe.TypeDescribe;
import com.ziiwee.reduce.describe.annotations.Label;
import com.ziiwee.reduce.describe.models.TypeDescribeModel;
import com.ziiwee.reduce.describe.types.TypeProvider;
import com.ziiwee.reduce.describe.types.TypeProviderContext;
import com.ziiwee.reduce.describe.utils.DescribeUtil;
import lombok.Data;
import org.springframework.core.ResolvableType;
import org.springframework.data.domain.Pageable;

import java.util.List;

/**
 * @author ziiwee
 * @date 2020/8/13
 */
public class PageableTypeProvider implements TypeProvider {
    @Override
    public boolean support(ResolvableType type) {
        return ReflectUtil.isFromInterface(type.resolve(), Pageable.class);
    }

    @Override
    public TypeDescribe render(ResolvableType type, TypeProviderContext context) {
        TypeDescribe model = context.render(ResolvableType.forClass(PageableModel.class));
        for (FieldDescribe field : model.getFields()) {
            context.render(field.getType());
        }
        TypeDescribeModel meta = new TypeDescribeModel(
                DescribeUtil.getSimpleNameByResolvableType(type),
                type,
                "分页信息",
                null,
                model.getFields()
        );
//        context.putCache(type.toString(), meta);
        return meta;
    }

    @Data
    public static class PageableModel {
        @Label(value = "页数", desc = "当前第几页(最小值为0)")
        private Integer page;
        @Label(value = "条数", desc = "每页多少条")
        private Integer size;
        @Label(value = "排序", desc = "如: sort=id 则表示通过 id 顺序 , sort=id,desc 则表示通过 id 倒序")
        private List<String> sort;
    }
}
